package com.agger.harmonyosthemeseditor.userinterface;

import com.agger.harmonyosthemeseditor.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import java.util.List;

public class TitleSwiper extends PageSliderProvider {
    private boolean Complete = false;
    private Object[] objects;

    //数据实体类
    public static class DataItem{
        String mText;
        public DataItem(String text) {
            mText = text;
        }
    }
    // 数据源，每个页面对应list中的一项
    private List<DataItem> list;
    private Context mContext;

    public TitleSwiper(List<DataItem> list, Context context) {
        this.list = list;
        this.mContext = context;
        this.objects = new Object[list.size()];
    }
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        if(!Complete)
        {
            final DataItem data = list.get(i);
            DisplayAttributes displayAttributes = DisplayManager.getInstance().getDefaultDisplay(mContext).get().getAttributes();
            DirectionalLayout directionalLayout = new DirectionalLayout(mContext);
            directionalLayout.setHeight(DirectionalLayout.LayoutConfig.MATCH_PARENT);
            directionalLayout.setWidth(DirectionalLayout.LayoutConfig.MATCH_PARENT);
            directionalLayout.setAlignment(LayoutAlignment.CENTER);
            directionalLayout.setOrientation(Component.VERTICAL);
            Text text = new Text(mContext);
            text.setHeight(DirectionalLayout.LayoutConfig.MATCH_PARENT);
            text.setWidth(DirectionalLayout.LayoutConfig.MATCH_PARENT);
            text.setTextColor(new Color(ResourceTool.getColor(mContext, ResourceTable.Color_dawnlighti_actionbar_text,0xF9A01E)));
            text.setTextSize((int)((30f*displayAttributes.scalDensity)+0.5f));
            text.setPaddingLeft((int)((24f*displayAttributes.scalDensity)+0.5f));
            text.setPaddingRight((int)((24f*displayAttributes.scalDensity)+0.5f));
            text.setTruncationMode(Text.TruncationMode.ELLIPSIS_AT_END);
            text.setTextAlignment(TextAlignment.LEFT|TextAlignment.VERTICAL_CENTER);
            text.setText(data.mText);
            directionalLayout.addComponent(text);
            componentContainer.addComponent(directionalLayout);
            objects[i] = directionalLayout;
            if(i == (list.size()-1))Complete = true;
            return directionalLayout;
        }
        else return objects[i];
    }
    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        //componentContainer.removeComponent((Component) o);
    }
    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        //可添加具体处理逻辑
        return true;
    }
}
